/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command.preview
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import multimedia.control.business.XMLDelegate;
	import multimedia.control.event.preview.LoadDFXPXMLEvent;
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.CaptionSetVO;
	import multimedia.model.vo.CaptionVO;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	/**
	 * 	LoadDFXPXMLCommand
	 * 
	 *	@author jemden
	 *	@version 1.0
	 */
	public class LoadDFXPXMLCommand extends SequenceCommand implements ICommand, IResponder
	{
		private var model:ModelLocator;
		
		private var e:LoadDFXPXMLEvent;
		
		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = event as LoadDFXPXMLEvent;
			this.nextEvent = e.nextEvent;
			if(e.video.subtitles != null && e.video.subtitles != "")
			{
				var service:XMLDelegate = new XMLDelegate(this);
				service.loadDFXP(e.video.subtitles);
			}
			else
			{
				executeNextCommand();
			}
		}
		
		public function result(data:Object):void
		{	
			var captionSets:ArrayCollection = new ArrayCollection();
			var xData:XMLList = new XMLList(data.result);

			var divs:XMLList = xData.children().children();
			var div:XML;
			for each(div in divs) 
			{
				var lang:String = div.attributes()[0]; //div.attribute("xml:lang");  // Funktioniert nicht
				var style:String = div.attributes()[1]; //div.attribute("style");
			  	
			  	if(lang != null)
			  	{
			  		var captionSet:CaptionSetVO = new CaptionSetVO();
			  		captionSet.lang = lang;
			  		captionSet.style = style;
			  		
				  //	model.log += lang +" " + style + "\n";	
				    
				    var ps:XMLList = div.children();
					var p:XML;
					for each(p in ps) 
					{
						var begin:String = p.attribute("begin");  
						var end:String = p.attribute("end");
						var text:String;
						
						if (p.hasSimpleContent())
							text = p.toString();
						else
							text = p.children().toString();
						
						text = text.replace(new RegExp("\n","g"), "");		// Replace All line breaks
						
						var caption:CaptionVO = new CaptionVO();
						caption.begin = stringToNumber(begin) / 1000;
						caption.end = stringToNumber(end) / 1000;
						caption.text = text;
						
						captionSet.captions.addItem(caption);
					//	model.log += caption.begin + " " + caption.end + " " + caption.text +"\n";	
					}
					captionSets.addItem(captionSet);
			  	}
			}
			
			e.video.captionSets = captionSets;
			
			executeNextCommand();
		}
		
		/*
		 *	TODO: Diese Funktionalität durch Testfälle überprüfen
		 */
		public function stringToNumber(timestamp:String):Number
		{
			var result:Number = 0.0;
			var parts:Array = timestamp.split(":");
			
			for(var i:int=parts.length-1; i>=0; i--)
			{
				switch (i)
				{
					case parts.length-1:
						var secondParts:Array = String(parts[i]).split(".");
						result += secondParts[0]*1000;
						
						if(secondParts[1] != null)
							if(secondParts[1] > 10)
								result += secondParts[1]*10;
							else if(secondParts[1] > 0)
								result += secondParts[1]*100;
						
					break;
					case parts.length-2:
						result += parts[i]*60000;
					break;
					case parts.length-3:
						result += parts[i]*360000;
					break;
				}
			}
			
			return result;
		}
		
		
		public function fault(info:Object):void
		{
			
		}
	}
}